package com.briup.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.briup.DTO.AddOrUpdateIndexRecordDTO;
import com.briup.enums.CodeAndMessageEnum;
import com.briup.exception.SystemException;
import com.briup.mapper.IndexRecordMapper;
import com.briup.pojo.IndexRecord;
import com.briup.result.PageTemplate;
import com.briup.result.Result;
import com.briup.service.IndexRecordService;
import com.briup.util.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @author Lenovo
 * @description 针对表【index_record(动物指标记录表)】的数据库操作Service实现
 * @createDate 2024-03-17 17:44:00
 */
@Service
public class IndexRecordServiceImpl extends ServiceImpl<IndexRecordMapper, IndexRecord>
        implements IndexRecordService {

    @Autowired
    private IndexRecordMapper indexRecordMapper;

    @Override
    public PageTemplate indexQuery(Integer pageNum, Integer pageSize, String startWeight, String endWeight) throws SystemException {
        //创建条件选择器
        LambdaQueryWrapper<IndexRecord> indexRecordLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //查询体重区间的数据
        //输入最小值，大于等于最小值（ge方法：大于等于  gt方法：大于）
        indexRecordLambdaQueryWrapper.ge(Objects.nonNull(startWeight), IndexRecord::getIrdWeight, startWeight);
        //输入最大值，小于等于最大值（le方法：小于等于  lt方法：小于）
        indexRecordLambdaQueryWrapper.le(Objects.nonNull(endWeight), IndexRecord::getIrdWeight, endWeight);
        Page<IndexRecord> IndexPage = new Page<>(pageNum, pageSize);
        indexRecordMapper.selectPage(IndexPage, indexRecordLambdaQueryWrapper);
        return new PageTemplate(IndexPage.getTotal(), IndexPage.getRecords());
    }

    @Override
    public Result deleteById(String id) throws SystemException {
        IndexRecord indexRecord = indexRecordMapper.selectById(id);
        if (Objects.isNull(indexRecord)) {
            throw new SystemException(CodeAndMessageEnum.INDEX_RECORD_IS_NULL);
        }
        indexRecordMapper.deleteById(id);
        return Result.success();
    }

    @Override
    public Result deleteByIdAll(List<String> ids) throws SystemException {
        LambdaQueryWrapper<IndexRecord> indexRecordLambdaQueryWrapper = new LambdaQueryWrapper<>();
        indexRecordLambdaQueryWrapper.in(Objects.nonNull(ids) && !ids.isEmpty(), IndexRecord::getIrdAnimalId, ids);
        List<IndexRecord> indexRecords = indexRecordMapper.selectList(indexRecordLambdaQueryWrapper);
        if (ids.size() != indexRecords.size()) {
//            有id找不到对应的信息
            throw new SystemException(CodeAndMessageEnum.INDEX_RECORD_IS_NULL);
        }
        indexRecordMapper.deleteBatchIds(ids);
        return Result.success();
    }

    @Override
    public Result indexSaveOrUpdate(AddOrUpdateIndexRecordDTO addOrUpdateIndexRecordDTO) {
        //bean拷贝
        IndexRecord indexRecord = BeanCopyUtils.copyBean(addOrUpdateIndexRecordDTO, IndexRecord.class);
        if (Objects.isNull(addOrUpdateIndexRecordDTO.getIrdAnimalId())) {
            indexRecordMapper.insert(indexRecord);
            return Result.success();
        } else {
            indexRecordMapper.updateById(indexRecord);
            return Result.success();
        }
    }
}




